﻿using AutoMapper;
using B.S.BaseData.Api.Write.Applicantion.Command.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.RBAC;
using MediatR;
using Yitter.IdGenerator;

namespace B.S.BaseData.Api.Write.Applicantion.CommandHandler.RBAC
{
    public class DelPermissionHandler : IRequestHandler<DelPermissionCommand, APIResult<int>>
    {
        private readonly IBaseRepository<PermissionsModel> permissionRepo;
        private readonly IMapper mapper;

        public DelPermissionHandler(IBaseRepository<PermissionsModel> permissionRepo, IMapper mapper)
        {
            this.permissionRepo = permissionRepo;
            this.mapper = mapper;
        }

        public Task<APIResult<int>> Handle(DelPermissionCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> res = new APIResult<int>();
            var permission= permissionRepo.GetAll().Where(x=>x.Id==request.Id).FirstOrDefault();
            if (permission == null)
            {
                res.Code = ApiEnum.Fail;
                res.Msg = "你要删除的编号不存在";
                return Task.FromResult(res);
            }
            permission.IsDeleted= true;
            var list=permissionRepo.UpdateAsync(permission).Result;
            if (list > 0)
            {
                res.Code = ApiEnum.Success;
                res.Msg = "删除成功";
                res.Data= list;
            }
            else
            {
                res.Code = ApiEnum.Fail;
                res.Msg = "删除失败";
                res.Data = list;
            }
            return Task.FromResult(res);
        }
    }
}
